package com.icss.ah.fullGift.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.EasyuiDataGridJson;

@Service
@Transactional
public class FullGiftService {
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private EasyuiDataGridJson dataGridJson = new EasyuiDataGridJson();
	
	/**
	 * 满赠列表查询
	 */
	public EasyuiDataGridJson queryFullGiftList(Map<String, String> map){
		try {
			List<Map<String, Object>> rows = sqlSessionTemplate.selectList("FullGiftBean.queryFullGiftList",map);
			Long total = sqlSessionTemplate.selectOne("FullGiftBean.queryFullGiftListCount",map);
			dataGridJson.setRows(rows);
			dataGridJson.setTotal(total);
			infolog.info("FullGiftService|queryFullGiftList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullGiftService|queryFullGiftList", e);
		}
		return dataGridJson;
	}
	
	/**
	 * 满赠信息保存
	 */
	public boolean save(Map<String, String> map,List<Map<String, String>> list){
		boolean result = false;
		try {
			String flag = map.get("flag");
			if(flag!=null&&"add".equals(flag)){
				sqlSessionTemplate.insert("FullGiftBean.saveFullGiftInfo",map);
			}else{
				sqlSessionTemplate.update("FullGiftBean.updateFullGiftInfo",map);
				sqlSessionTemplate.delete("FullGiftBean.deleteFullGiftProduct",map);
			}
			if(list.size()>0)
				sqlSessionTemplate.insert("FullGiftBean.saveFullGiftProduct",list);
			infolog.info("FullGiftService|save");
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullGiftService|save", e);
		}
		return result;
	}	
	
	/**
	 * 满赠信息状态修改
	 */
	public void updateFullGiftInfoStatus(String fgId){
		try {
			sqlSessionTemplate.update("FullGiftBean.updateFullGiftInfoStatus",fgId);
			infolog.info("FullGiftService|updateFullGiftInfoStatus");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullGiftService|updateFullGiftInfoStatus", e);
		}
	}
	
	/**
	 * 满赠信息详情查询
	 */
	public Map<String, Object> queryFullGiftInfo(Map<String, String> map){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result = sqlSessionTemplate.selectOne("FullGiftBean.queryFullGiftInfo",map);
			infolog.info("FullGiftService|queryFullGiftInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullGiftService|queryFullGiftInfo", e);
		}
		return result;
	}
	
	/**
	 * 店铺商品列表
	 */
	public List<Map<String, Object>> queryProductList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("FullGiftBean.queryProductList",map);
			infolog.info("FullGiftService|queryProductList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullGiftService|queryProductList", e);
		}
		return list;
	}
	
	public List<Map<String, Object>> queryProductTypeList(Map<String, String> map){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = sqlSessionTemplate.selectList("FullGiftBean.queryProductTypeList",map);
			infolog.info("FullGiftService|queryProductTypeList");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullGiftService|queryProductTypeList", e);
		}
		return list == null ? new ArrayList<Map<String,Object>>() : list;
	}
	
	/**
	 * 删除满赠信息
	 * @param rdeId
	 * @return
	 */
	public String delInfo(String fgId){
		String result = "f";
		try {
//			sqlSessionTemplate.delete("FullGiftBean.delInfo",fgId);
//			sqlSessionTemplate.delete("FullGiftBean.delProInfo",fgId);
			sqlSessionTemplate.update("FullGiftBean.upInfo",fgId);
			result = "t";
			infolog.info("FullGiftService|delInfo");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("FullGiftService|delInfo", e);
		}
		return result;
	}

}
